﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Data.SqlClient;
using System.Drawing.Drawing2D;
using System.Drawing.Printing;
using System.Linq;
using System.Net.Http;
using System.Text;
using System.Threading.Tasks;
using System.Transactions;
using System.Web;
using System.Web.Http;
using System.Web.UI.WebControls;
using System.Windows.Forms;
using CloudApiServer.Services;
using Google.Protobuf.WellKnownTypes;
using log4net;
using MySql.Data.MySqlClient;
using Newtonsoft.Json;
using Newtonsoft.Json.Linq;
using Org.BouncyCastle.Asn1.Cmp;
using Org.BouncyCastle.Asn1.X500;
using Org.BouncyCastle.Asn1.X509;
using static System.Windows.Forms.VisualStyles.VisualStyleElement.ListView;
using static log4net.Appender.RollingFileAppender;

namespace CloudApiServer.Controllers
{
    /// <summary>
    ///江苏江西的控制器，都在csb目录下
    /// </summary>
    [RoutePrefix("csb")]
    public class FsiApiJiangsuJiangxiController : ApiController
    {
        private readonly SignInSignOutService _signInSignOuService;
        private readonly FsiPsnInfoService _fsiPsnInfoService;
        private readonly FsiDrugStoreService _fsiDrugStoreService;
        private readonly CatalogQueryService _catalogQueryService;
        private readonly FsiIntegratedQueryService _fsiIntegratedQueryService;
        private readonly YbSettlementStmtService _ybSettlementStmtService;
        private readonly RiskConService _riskConService;
        private readonly FileuploadService _fileuploadService;
        private readonly GoodsService _goodsService;
        private readonly OutpatientDocInfoService _outpatientDocInfoService;
        private readonly FsiReverseService _fsiReverseService;
        public FsiApiJiangsuJiangxiController()
        {
            _signInSignOuService = new SignInSignOutService();
            _fsiPsnInfoService = new FsiPsnInfoService();
            _fsiDrugStoreService = new FsiDrugStoreService();
            _catalogQueryService = new CatalogQueryService();
            _fsiIntegratedQueryService = new FsiIntegratedQueryService();
            _ybSettlementStmtService = new YbSettlementStmtService();
            _riskConService = new RiskConService();
            _fileuploadService = new FileuploadService();
            _goodsService = new GoodsService();
            _outpatientDocInfoService = new OutpatientDocInfoService();
            _fsiReverseService = new FsiReverseService();
        }
        /*接口*/
        /// <summary>
        /// 医保接口没找到的统一接口返回
        /// </summary>
        /// <param name="request"></param>
        /// <returns></returns>
        [HttpGet]
        [HttpPost]
        public IHttpActionResult FsiNotFound(HttpRequestMessage request)
        {
            string url = request.RequestUri.ToString();
            string Method = request.Method.ToString();
            Console.WriteLine($"请求接口{url}\r\n请求入参{request.Content.ReadAsStringAsync().Result}");
            string Errnum = LogHelper.GetErrornumber();
            try
            {
                if (string.IsNullOrEmpty(request.Content.ReadAsStringAsync().Result))
                {
                    FsiHeader<outputsignInOut> FsiHeader = new FsiHeader<outputsignInOut>
                    {
                        infcode = -1,
                        err_msg = $"[{Errnum}]调用江苏/江西医保自定义服务失败！请求的接口【{url}】方法【{Method}】暂未添加且入参为空！当前请求入参：" + request.Content.ReadAsStringAsync().Result
                    };
                    return Json(FsiHeader);
                }
                else
                {
                    var fsiRequest = JsonConvert.DeserializeObject<FsiRequestHeader<InputSignIn>>(request.Content.ReadAsStringAsync().Result);
                    string infno = fsiRequest.infno;
                    FsiHeader<outputsignInOut> FsiHeader = new FsiHeader<outputsignInOut>
                    {
                        infcode = -1,
                        err_msg = $"[{Errnum}]调用江苏/江西医保自定义服务失败！请求的接口【{url}】方法【{Method}】暂未添加！当前请求的接口号：{infno},当前请求入参：" + request.Content.ReadAsStringAsync().Result
                    };
                    return Json(FsiHeader);
                }
            }
            catch (JsonReaderException)
            {
                FsiHeader<outputsignInOut> FsiHeader = new FsiHeader<outputsignInOut>
                {
                    infcode = -1,
                    err_msg = $"[{Errnum}]调用江苏/江西医保自定义服务失败！请求的接口【{url}】方法【{Method}】暂未添加且入参格式不正确！服务端无法正常反序列化！请检查入参是否为Json格式！当前请求入参：" + request.Content.ReadAsStringAsync().Result
                };
                return Json(FsiHeader);

            }
            catch (Exception ex)
            {
                FsiHeader<outputsignInOut> FsiHeader = new FsiHeader<outputsignInOut>
                {
                    infcode = -1,
                    err_msg = $"[{Errnum}]调用江苏/江西医保自定义服务失败！请求的接口【{url}】方法【{Method}】暂未添加且服务端程序处理异常！请联系管理员排查！当前异常提示：{ex.Message},当前请求入参：" + request.Content.ReadAsStringAsync().Result
                };
                return Json(FsiHeader);
            }
        }
        /// <summary>
        /// 9001签到
        /// </summary>
        /// <returns></returns>
        [Route("9001")]
        [Route("hsa-fsi-9001")]
        [HttpPost]
        public async Task<IHttpActionResult> signIn(HttpRequestMessage request)
        {
            try
            {
                if (FsiDatainfo.stateFsiApi == false)
                {
                    throw new Exception("该接口已被控制台停用！请联系管理员！");
                }
                var fsiRequest = JsonConvert.DeserializeObject<FsiRequestHeader<InputSignIn>>(await request.Content.ReadAsStringAsync());
                string fsiadmvs = fsiRequest.mdtrtarea_admvs.Substring(0, 2);
                Random rnd = new Random();
                DateTime dateTime = DateTime.Now;
                var signin = await _signInSignOuService.SignInAsync(fsiRequest, this.GetType().Name);
                var response = new FsiHeader<outputsignInOut>
                {
                    output = signin,
                    infcode = 0,
                    warn_msg = null,
                    cainfo = null,
                    err_msg = "success",
                    refmsg_time = dateTime.ToString("yyyyMMddHHmmssfff"),
                    signtype = null,
                    respond_time = dateTime.ToString("yyyyMMddHHmmssfff"),
                    inf_refmsgid = $"{fsiadmvs}0000{dateTime.ToString("yyyyMMddHHmmssfff")}{rnd.Next(1000000, 10000000).ToString()}"
                };
                return Json(response);

            }
            catch (Exception ex)
            {
                string Errnum = LogHelper.GetErrornumber();
                var FsiHeader = new FsiHeader<outputsignInOut>
                {
                    infcode = -1,
                    err_msg = $"[{Errnum}]调用江苏/江西医保自定义服务失败！请检查！错误内容：{ex.Message} 异常来源：{ex.StackTrace}"
                };
                return Json(FsiHeader);
            }
        }
        /// <summary>
        /// 9002签退
        /// </summary>
        /// <returns></returns>
        [Route("9002")]
        [Route("hsa-fsi-9002")]
        [HttpPost]
        public async Task<IHttpActionResult> signOut(HttpRequestMessage request)
        {

            try
            {
                if (FsiDatainfo.stateFsiApi == false)
                {
                    throw new Exception("该接口已被控制台停用！请联系管理员！");
                }
                var fsiRequest = JsonConvert.DeserializeObject<FsiRequestHeader<InputSignOut>>(request.Content.ReadAsStringAsync().Result);
                string fsiadmvs = fsiRequest.mdtrtarea_admvs.Substring(0, 2);
                Random rnd = new Random();
                DateTime dateTime = DateTime.Now;
                var siginout = await _signInSignOuService.SignOutAsync(fsiRequest, this.GetType().Name);
                var FsiHeader = new FsiHeader<outputsignInOut>
                {
                    output = siginout,
                    infcode = 0,
                    warn_msg = null,
                    cainfo = null,
                    err_msg = "success",
                    refmsg_time = dateTime.ToString("yyyyMMddHHmmssfff"),
                    signtype = null,
                    respond_time = dateTime.ToString("yyyyMMddHHmmssfff"),
                    inf_refmsgid = $"{fsiadmvs}0000{dateTime.ToString("yyyyMMddHHmmssfff")}{rnd.Next(1000000, 10000000).ToString()}"
                };
                return Json(FsiHeader);
            }
            catch (Exception ex)
            {
                string Errnum = LogHelper.GetErrornumber();
                var FsiHeader = new FsiHeader<outputsignInOut>
                {
                    infcode = -1,
                    err_msg = $"[{Errnum}]调用江苏/江西医保自定义服务失败！请检查！错误内容：{ex.Message} 异常来源：{ex.StackTrace}"
                };
                return Json(FsiHeader);
            }
        }

        /// <summary>
        /// 1101
        /// </summary>
        /// <returns></returns>
        [Route("1101")]
        [Route("hsa-fsi-1101")]
        [HttpPost]
        public async Task<IHttpActionResult> queryPsnInfo(HttpRequestMessage request)
        {
            try
            {
                if (FsiDatainfo.stateFsiApi == false)
                {
                    throw new Exception("该接口已被控制台停用！请联系管理员！");
                }
                var fsiRequest = JsonConvert.DeserializeObject<FsiRequestHeader<InputQueryPsnInfoRequest>>(request.Content.ReadAsStringAsync().Result);
                string fsiadmvs = fsiRequest.mdtrtarea_admvs.Substring(0, 2);
                Random rnd = new Random();
                DateTime dateTime = DateTime.Now;
                if (FsiDatainfo.statesignon == true)
                {
                    bool sign = await _signInSignOuService.SignInStateAsync(fsiRequest.fixmedins_code, fsiRequest.opter, fsiRequest.sign_no);
                    if (sign == false)
                    {
                        throw new Exception("校验签到信息失败：未查询到有效的签到信息！");
                    }
                }
                var queryPsnInfo = await _fsiPsnInfoService.QueryPsnInfo(fsiRequest, this.GetType().Name);
                var FsiHeader = new FsiHeader<outputQueryPsnInfo>
                {
                    output = queryPsnInfo,
                    infcode = 0,
                    warn_msg = null,
                    cainfo = null,
                    err_msg = "success",
                    refmsg_time = dateTime.ToString("yyyyMMddHHmmssfff"),
                    signtype = null,
                    respond_time = dateTime.ToString("yyyyMMddHHmmssfff"),
                    inf_refmsgid = $"{fsiadmvs}0000{dateTime.ToString("yyyyMMddHHmmssfff")}{rnd.Next(1000000, 10000000).ToString()}"
                };
                return Json(FsiHeader);

            }
            catch (Exception ex)
            {
                string Errnum = LogHelper.GetErrornumber();
                var FsiHeader = new FsiHeader<outputsignInOut>
                {
                    infcode = -1,
                    err_msg = $"[{Errnum}]调用江苏/江西医保自定义服务失败！请检查！错误内容：{ex.Message} 异常来源：{ex.StackTrace}"
                };
                return Json(FsiHeader);
            }

        }
        /// <summary>
        /// 2101
        /// </summary>
        /// <returns></returns>
        [Route("2101")]
        [Route("hsa-fsi-2101")]
        [HttpPost]
        public async Task<IHttpActionResult> drugstoreSettleAccountsPre(HttpRequestMessage request)
        {
            try
            {
                if (FsiDatainfo.stateFsiApi == false)
                {
                    throw new Exception("该接口已被控制台停用！请联系管理员！");
                }
                var fsiRequest = JsonConvert.DeserializeObject<FsiRequestHeader<InputDrugstoreSettleAccountsPreA>>(request.Content.ReadAsStringAsync().Result);
                if (FsiDatainfo.statesignon == true)
                {
                    bool sign = await _signInSignOuService.SignInStateAsync(fsiRequest.fixmedins_code, fsiRequest.opter, fsiRequest.sign_no);
                    if (sign == false)
                    {
                        throw new Exception("校验签到信息失败：未查询到有效的签到信息！");
                    }
                }
                string fsiadmvs = fsiRequest.mdtrtarea_admvs.Substring(0, 2);
                Random rnd = new Random();
                DateTime dateTime = DateTime.Now;
                var drugstoreSettleAccountsPreA = await _fsiDrugStoreService.DrugstoreSettleAccountsPre(fsiRequest, this.GetType().Name);
                var FsiHeader = new FsiHeader<outputDrugstoreSettleAccountsPreA>
                {
                    output = drugstoreSettleAccountsPreA,
                    infcode = 0,
                    warn_msg = null,
                    cainfo = null,
                    err_msg = "success",
                    refmsg_time = dateTime.ToString("yyyyMMddHHmmssfff"),
                    signtype = null,
                    respond_time = dateTime.ToString("yyyyMMddHHmmssfff"),
                    inf_refmsgid = $"{fsiadmvs}0000{dateTime.ToString("yyyyMMddHHmmssfff")}{rnd.Next(1000000, 10000000).ToString()}"
                };
                return Json(FsiHeader);
            }
            catch (Exception ex)
            {
                string Errnum = LogHelper.GetErrornumber();
                var FsiHeader = new FsiHeader<outputsignInOut>
                {
                    infcode = -1,
                    err_msg = $"[{Errnum}]调用江苏/江西医保自定义服务失败！请检查！错误内容：{ex.Message} 异常来源：{ex.StackTrace}"
                };
                return Json(FsiHeader);
            }
        }
        /// <summary>
        /// 2102
        /// </summary>
        /// <returns></returns>
        [Route("2102")]
        [Route("hsa-fsi-2102")]
        [HttpPost]
        public async Task<IHttpActionResult> drugstoreSettleAccounts(HttpRequestMessage request)
        {
            try
            {
                if (FsiDatainfo.stateFsiApi == false)
                {
                    throw new Exception("该接口已被控制台停用！请联系管理员！");
                }
                var fsiRequest = JsonConvert.DeserializeObject<FsiRequestHeader<InputDrugstoreSettleAccountsPreA>>(request.Content.ReadAsStringAsync().Result);
                if (FsiDatainfo.statesignon == true)
                {
                    if (string.IsNullOrEmpty(fsiRequest.sign_no))
                    {
                        throw new Exception("校验签到信息失败：传入的签到号为空！");
                    }
                    bool sign = await PublicMethod.FsisignIn(fsiRequest.fixmedins_code, fsiRequest.opter, fsiRequest.sign_no);

                    if (sign == false)
                    {
                        throw new Exception("校验签到信息失败：未查询到有效的签到信息！");
                    }
                }
                string fsiadmvs = fsiRequest.mdtrtarea_admvs.Substring(0, 2);
                Random rnd = new Random();
                DateTime dateTime = DateTime.Now;
                var drugstoreSettleAccountsA = await _fsiDrugStoreService.DrugstoreSettleAccounts(fsiRequest, this.GetType().Name);
                var FsiHeader = new FsiHeader<outputDrugstoreSettleAccountsPreA>
                {
                    output = drugstoreSettleAccountsA,
                    infcode = 0,
                    warn_msg = null,
                    cainfo = null,
                    err_msg = "success",
                    refmsg_time = dateTime.ToString("yyyyMMddHHmmssfff"),
                    signtype = null,
                    respond_time = dateTime.ToString("yyyyMMddHHmmssfff"),
                    inf_refmsgid = $"{fsiadmvs}0000{dateTime.ToString("yyyyMMddHHmmssfff")}{rnd.Next(1000000, 10000000).ToString()}"
                };
                return Json(FsiHeader);

            }
            catch (Exception ex)
            {
                string Errnum = LogHelper.GetErrornumber();
                var FsiHeader = new FsiHeader<outputsignInOut>
                {
                    infcode = -1,
                    err_msg = $"[{Errnum}]调用江苏/江西医保自定义服务失败！请检查！错误内容：{ex.Message} 异常来源：{ex.StackTrace}"
                };
                return Json(FsiHeader);
            }
        }

        
        /// <summary>
        /// 2601冲正交易
        /// </summary>
        /// <param name="request"></param>
        /// <returns></returns>
        [Route("2601")]
        [Route("hsa-fsi-2601")]
        [HttpPost]
        public async Task<IHttpActionResult> revsMethod(HttpRequestMessage request)
        {
            try
            {
                if (FsiDatainfo.stateFsiApi == false)
                {
                    throw new Exception("该接口已被控制台停用！请联系管理员！");
                }

                var fsiRequest =
                    JsonConvert.DeserializeObject<FsiRequestHeader<InputRevsMethod>>(request.Content
                        .ReadAsStringAsync().Result);
                string fsiadmvs = fsiRequest.mdtrtarea_admvs.Substring(0, 2);
                if (FsiDatainfo.statesignon == true)
                {
                    bool sign = await _signInSignOuService.SignInStateAsync(fsiRequest.fixmedins_code, fsiRequest.opter,
                        fsiRequest.sign_no);
                    if (sign == false)
                    {
                        throw new Exception("校验签到信息失败：未查询到有效的签到信息！");
                    }
                }

                Random rnd = new Random();
                DateTime dateTime = DateTime.Now;
                bool flag = await _fsiReverseService.RevsMethod(fsiRequest, this.GetType().Name);
                if (flag == true)
                {
                    FsiHeader<outputsignInOut> FsiHeader = new FsiHeader<outputsignInOut>
                    {
                        output = null,
                        infcode = 0,
                        warn_msg = null,
                        cainfo = null,
                        err_msg = "success",
                        refmsg_time = dateTime.ToString("yyyyMMddHHmmssfff"),
                        signtype = null,
                        respond_time = dateTime.ToString("yyyyMMddHHmmssfff"),
                        inf_refmsgid =
                            $"{fsiadmvs}0000{dateTime.ToString("yyyyMMddHHmmssfff")}{rnd.Next(1000000, 10000000).ToString()}"
                    };
                    return Json(FsiHeader);
                }
                else
                {
                    string Errnum = LogHelper.GetErrornumber();
                    var FsiHeader = new FsiHeader<outputsignInOut>
                    {
                        infcode = -1,
                        err_msg =
                            $"[{Errnum}]调用江苏/江西医保自定义服务失败！请检查！调用冲正服务FsiReverseService.RevsMethod返回的状态为{flag}！"
                    };
                    return Json(FsiHeader);
                }
            }
            catch (Exception ex)
            {
                string Errnum = LogHelper.GetErrornumber();
                var FsiHeader = new FsiHeader<outputsignInOut>
                {
                    infcode = -1,
                    err_msg = $"[{Errnum}]调用江苏/江西医保自定义服务失败！请检查！错误内容：{ex.Message} 异常来源：{ex.StackTrace}"
                };
                return Json(FsiHeader);
            }
        }
        
        /// <summary>
        /// 2103退款接口
        /// </summary>
        /// <returns></returns>
        [Route("2103")]
        [Route("hsa-fsi-2103")]
        [HttpPost]
        public async Task<IHttpActionResult> drugstoreSettleRevoke(HttpRequestMessage request)
        {
            try
            {
                if (FsiDatainfo.stateFsiApi == false)
                {
                    throw new Exception("该接口已被控制台停用！请联系管理员！");
                }
                var fsiRequest = JsonConvert.DeserializeObject<FsiRequestHeader<InputDrugstoreSettleRevokeRequest>>(request.Content.ReadAsStringAsync().Result);
                if (FsiDatainfo.statesignon == true)
                {
                    bool sign = await _signInSignOuService.SignInStateAsync(fsiRequest.fixmedins_code, fsiRequest.opter, fsiRequest.sign_no);
                    if (sign == false)
                    {
                        throw new Exception("校验签到信息失败：未查询到有效的签到信息！");
                    }
                }
                string fsiadmvs = fsiRequest.mdtrtarea_admvs.Substring(0, 2);
                Random rnd = new Random();
                DateTime dateTime = DateTime.Now;
                var drugstoreSettleRevoke = await _fsiDrugStoreService.DrugstoreSettleRevoke(fsiRequest, this.GetType().Name);
                var FsiHeader = new FsiHeader<outputDrugstoreSettleRevoke>
                {
                    output = drugstoreSettleRevoke,
                    infcode = 0,
                    warn_msg = null,
                    cainfo = null,
                    err_msg = "success",
                    refmsg_time = dateTime.ToString("yyyyMMddHHmmssfff"),
                    signtype = null,
                    respond_time = dateTime.ToString("yyyyMMddHHmmssfff"),
                    inf_refmsgid = $"{fsiadmvs}0000{dateTime.ToString("yyyyMMddHHmmssfff")}{rnd.Next(1000000, 10000000).ToString()}"
                };
                return Json(FsiHeader);
            }
            catch (Exception ex)
            {
                string Errnum = LogHelper.GetErrornumber();
                var FsiHeader = new FsiHeader<outputsignInOut>
                {
                    infcode = -1,
                    err_msg = $"[{Errnum}]调用江苏/江西医保自定义服务失败！请检查！错误内容：{ex.Message} 异常来源：{ex.StackTrace}"
                };
                return Json(FsiHeader);
            }
        }

        /// <summary>
        /// 3301上传对照
        /// </summary>
        /// <param name="request"></param>
        /// <returns></returns>
        [Route("3301")]
        [Route("hsa-fsi-3301")]
        [HttpPost]
        public async Task<IHttpActionResult> catalogCompUp(HttpRequestMessage request)
        {
            try
            {
                if (FsiDatainfo.stateFsiApi == false)
                {
                    throw new Exception("该接口已被控制台停用！请联系管理员！");
                }
                var fsiRequest = JsonConvert.DeserializeObject<FsiRequestHeader<InputCatalogCompUp>>(request.Content.ReadAsStringAsync().Result);
                string fsiadmvs = fsiRequest.mdtrtarea_admvs.Substring(0, 2);
                if (FsiDatainfo.statesignon == true)
                {
                    bool sign = await _signInSignOuService.SignInStateAsync(fsiRequest.fixmedins_code, fsiRequest.opter, fsiRequest.sign_no);
                    if (sign == false)
                    {
                        throw new Exception("校验签到信息失败：未查询到有效的签到信息！");
                    }
                }
                Random rnd = new Random();
                DateTime dateTime = DateTime.Now;
                bool flag = await _catalogQueryService.CatalogCompUp(fsiRequest, this.GetType().Name);
                if (flag == true)
                {
                    FsiHeader<outputsignInOut> FsiHeader = new FsiHeader<outputsignInOut>
                    {
                        output = null,
                        infcode = 0,
                        warn_msg = null,
                        cainfo = null,
                        err_msg = "success",
                        refmsg_time = dateTime.ToString("yyyyMMddHHmmssfff"),
                        signtype = null,
                        respond_time = dateTime.ToString("yyyyMMddHHmmssfff"),
                        inf_refmsgid = $"{fsiadmvs}0000{dateTime.ToString("yyyyMMddHHmmssfff")}{rnd.Next(1000000, 10000000).ToString()}"
                    };
                    return Json(FsiHeader);
                }
                else
                {
                    string Errnum = LogHelper.GetErrornumber();
                    var FsiHeader = new FsiHeader<outputsignInOut>
                    {
                        infcode = -1,
                        err_msg = $"[{Errnum}]调用江苏/江西医保自定义服务失败！请检查！调用目录上传服务CatalogQueryService.CatalogCompUp返回的状态为{flag}！"
                    };
                    return Json(FsiHeader);
                }
            }
            catch (Exception ex)
            {
                string Errnum = LogHelper.GetErrornumber();
                var FsiHeader = new FsiHeader<outputsignInOut>
                {
                    infcode = -1,
                    err_msg = $"[{Errnum}]调用江苏/江西医保自定义服务失败！请检查！错误内容：{ex.Message} 异常来源：{ex.StackTrace}"
                };
                return Json(FsiHeader);
            }
        }
        /// <summary>
        /// 3302上传撤销
        /// </summary>
        /// <param name="request"></param>
        /// <returns></returns>
        [Route("3302")]
        [Route("hsa-fsi-3302")]
        [HttpPost]
        public async Task<IHttpActionResult> deleteCatalogCompAudited(HttpRequestMessage request)
        {
            try
            {
                if (FsiDatainfo.stateFsiApi == false)
                {
                    throw new Exception("该接口已被控制台停用！请联系管理员！");
                }
                var fsiRequest = JsonConvert.DeserializeObject<FsiRequestHeader<InputDeleteCatalogCompAudited>>(request.Content.ReadAsStringAsync().Result);
                string fsiadmvs = fsiRequest.mdtrtarea_admvs.Substring(0, 2);
                if (FsiDatainfo.statesignon == true)
                {
                    bool sign = await _signInSignOuService.SignInStateAsync(fsiRequest.fixmedins_code, fsiRequest.opter, fsiRequest.sign_no);
                    if (sign == false)
                    {
                        throw new Exception("校验签到信息失败：未查询到有效的签到信息！");
                    }
                }
                Random rnd = new Random();
                DateTime dateTime = DateTime.Now;
                bool flag = await _catalogQueryService.DeleteCatalogCompAudited(fsiRequest, this.GetType().Name);
                if (flag == true)
                {
                    FsiHeader<outputsignInOut> FsiHeader = new FsiHeader<outputsignInOut>
                    {
                        output = null,
                        infcode = 0,
                        warn_msg = null,
                        cainfo = null,
                        err_msg = "success",
                        refmsg_time = dateTime.ToString("yyyyMMddHHmmssfff"),
                        signtype = null,
                        respond_time = dateTime.ToString("yyyyMMddHHmmssfff"),
                        inf_refmsgid = $"{fsiadmvs}0000{dateTime.ToString("yyyyMMddHHmmssfff")}{rnd.Next(1000000, 10000000).ToString()}"
                    };
                    return Json(FsiHeader);
                }
                else
                {
                    string Errnum = LogHelper.GetErrornumber();
                    var FsiHeader = new FsiHeader<outputsignInOut>
                    {
                        infcode = -1,
                        err_msg = $"[{Errnum}]调用江苏/江西医保自定义服务失败！请检查！调用目录上传服务CatalogQueryService.DeleteCatalogCompAudited返回的状态为{flag}！"
                    };
                    return Json(FsiHeader);
                }
            }
            catch (Exception ex)
            {
                string Errnum = LogHelper.GetErrornumber();
                var FsiHeader = new FsiHeader<outputsignInOut>
                {
                    infcode = -1,
                    err_msg = $"[{Errnum}]调用江苏/江西医保自定义服务失败！请检查！错误内容：{ex.Message} 异常来源：{ex.StackTrace}"
                };
                return Json(FsiHeader);
            }
        }
        /// <summary>
        /// 1317上传信息查询
        /// </summary>
        /// <param name="request"></param>
        /// <returns></returns>
        [Route("1317")]
        [Route("hsa-fsi-1317")]
        [HttpPost]
        public async Task<IHttpActionResult> queryMedListMapByPage(HttpRequestMessage request)
        {
            try
            {
                if (FsiDatainfo.stateFsiApi == false)
                {
                    throw new Exception("该接口已被控制台停用！请联系管理员！");
                }
                var fsiRequest = JsonConvert.DeserializeObject<FsiRequestHeader<InputQueryMedListMapByPageRequest>>(request.Content.ReadAsStringAsync().Result);
                string fsiadmvs = fsiRequest.mdtrtarea_admvs.Substring(0, 2);
                if (FsiDatainfo.statesignon == true)
                {
                    bool sign = await _signInSignOuService.SignInStateAsync(fsiRequest.fixmedins_code, fsiRequest.opter, fsiRequest.sign_no);
                    if (sign == false)
                    {
                        throw new Exception("校验签到信息失败：未查询到有效的签到信息！");
                    }
                }
                DateTime dateTime = DateTime.Now;
                Random rnd = new Random();
                var outputQueryMedListMapByPage = await _catalogQueryService.QueryMedListMapByPage(fsiRequest, this.GetType().Name);
                FsiHeader<OutputQueryMedListMapByPage> FsiHeader = new FsiHeader<OutputQueryMedListMapByPage>
                {
                    output = outputQueryMedListMapByPage,
                    infcode = 0,
                    warn_msg = null,
                    cainfo = null,
                    err_msg = "success",
                    refmsg_time = dateTime.ToString("yyyyMMddHHmmssfff"),
                    signtype = null,
                    respond_time = dateTime.ToString("yyyyMMddHHmmssfff"),
                    inf_refmsgid = $"{fsiadmvs}0000{dateTime.ToString("yyyyMMddHHmmssfff")}{rnd.Next(1000000, 10000000).ToString()}"
                };
                return Json(FsiHeader);
            }
            catch (Exception ex)
            {
                string Errnum = LogHelper.GetErrornumber();
                var FsiHeader = new FsiHeader<outputsignInOut>
                {
                    infcode = -1,
                    err_msg = $"[{Errnum}]调用江苏/江西医保自定义服务失败！请检查！错误内容：{ex.Message} 异常来源：{ex.StackTrace}"
                };
                return Json(FsiHeader);
            }
        }
        /// <summary>
        /// 3201对账汇总
        /// </summary>
        /// <param name="request"></param>
        /// <returns></returns>
        [Route("3201")]
        [Route("hsa-fsi-3201")]
        [HttpPost]
        public async Task<IHttpActionResult> stmtTotal(HttpRequestMessage request)
        {
            try
            {
                var fsiRequest = JsonConvert.DeserializeObject<FsiRequestHeader<InputStmtTotalRequest>>(request.Content.ReadAsStringAsync().Result);
                if (FsiDatainfo.stateFsiApi == false)
                {
                    throw new Exception("该接口已被控制台停用！请联系管理员！");
                }
                if (FsiDatainfo.statesignon == true)
                {
                    bool sign = await _signInSignOuService.SignInStateAsync(fsiRequest.fixmedins_code, fsiRequest.opter, fsiRequest.sign_no);
                    if (sign == false)
                    {
                        throw new Exception("校验签到信息失败：未查询到有效的签到信息！");
                    }
                }
                string fsiadmvs = fsiRequest.mdtrtarea_admvs.Substring(0, 2);
                Random rnd = new Random();
                DateTime dateTime = DateTime.Now;
                var stmtinfo = await _ybSettlementStmtService.StmtTotal(fsiRequest, this.GetType().Name);
                var FsiHeader = new FsiHeader<OutputStmtTotal>
                {
                    output = stmtinfo,
                    infcode = 0,
                    warn_msg = null,
                    cainfo = null,
                    err_msg = "success",
                    refmsg_time = dateTime.ToString("yyyyMMddHHmmssfff"),
                    signtype = null,
                    respond_time = dateTime.ToString("yyyyMMddHHmmssfff"),
                    inf_refmsgid = $"{fsiadmvs}0000{dateTime.ToString("yyyyMMddHHmmssfff")}{rnd.Next(1000000, 10000000).ToString()}"
                };
                return Json(FsiHeader);
            }
            catch (Exception ex)
            {
                string Errnum = LogHelper.GetErrornumber();
                var FsiHeader = new FsiHeader<outputsignInOut>
                {
                    infcode = -1,
                    err_msg = $"[{Errnum}]调用江苏/江西医保自定义服务失败！请检查！错误内容：{ex.Message} 异常来源：{ex.StackTrace}"
                };
                return Json(FsiHeader);
            }
        }

        /// <summary>
        /// 3202对账明细 
        /// </summary>
        /// <param name="request"></param>
        /// <returns></returns>
        [Route("3201")]
        [Route("hsa-fsi-3201")]
        [HttpPost]
        public async Task<IHttpActionResult> stmtDetail(HttpRequestMessage request)
        {
            try
            {
                var fsiRequest = JsonConvert.DeserializeObject<FsiRequestHeader<InputStmtDetailRequest>>(request.Content.ReadAsStringAsync().Result);
                if (FsiDatainfo.stateFsiApi == false)
                {
                    throw new Exception("该接口已被控制台停用！请联系管理员！");
                }
                if (FsiDatainfo.statesignon == true)
                {
                    bool sign = await _signInSignOuService.SignInStateAsync(fsiRequest.fixmedins_code, fsiRequest.opter, fsiRequest.sign_no);
                    if (sign == false)
                    {
                        throw new Exception("校验签到信息失败：未查询到有效的签到信息！");
                    }
                }
                string fsiadmvs = fsiRequest.mdtrtarea_admvs.Substring(0, 2);
                Random rnd = new Random();
                DateTime dateTime = DateTime.Now;
                var fileinfo = await _ybSettlementStmtService.StmtDetail(fsiRequest, this.GetType().Name);
                var FsiHeader = new FsiHeader<OutputStmtDetail>
                {
                    output = fileinfo,
                    infcode = 0,
                    warn_msg = null,
                    cainfo = null,
                    err_msg = "success",
                    refmsg_time = dateTime.ToString("yyyyMMddHHmmssfff"),
                    signtype = null,
                    respond_time = dateTime.ToString("yyyyMMddHHmmssfff"),
                    inf_refmsgid = $"{fsiadmvs}0000{dateTime.ToString("yyyyMMddHHmmssfff")}{rnd.Next(1000000, 10000000).ToString()}"
                };
                return Json(FsiHeader);

            }
            catch (Exception ex)
            {
                string Errnum = LogHelper.GetErrornumber();
                var FsiHeader = new FsiHeader<outputsignInOut>
                {
                    infcode = -1,
                    err_msg = $"[{Errnum}]调用江苏/江西医保自定义服务失败！请检查！错误内容：{ex.Message} 异常来源：{ex.StackTrace}"
                };
                return Json(FsiHeader);
            }
        }
        
        /// <summary>
        /// 3262月度结算信息上传
        /// </summary>
        /// <param name="request"></param>
        /// <returns></returns>
        [Route("3262")] 
        public async Task<IHttpActionResult> monthlySettlementUpload(HttpRequestMessage request)
        {
            try
            {
                return Ok();
                /*
                var fsiRequest = JsonConvert.DeserializeObject<FsiRequestHeader<InputStmtDetailRequest>>(request.Content.ReadAsStringAsync().Result);
                if (FsiDatainfo.stateFsiApi == false)
                {
                    throw new Exception("该接口已被控制台停用！请联系管理员！");
                }
                if (FsiDatainfo.statesignon == true)
                {
                    bool sign = await _signInSignOuService.SignInStateAsync(fsiRequest.fixmedins_code, fsiRequest.opter, fsiRequest.sign_no);
                    if (sign == false)
                    {
                        throw new Exception("校验签到信息失败：未查询到有效的签到信息！");
                    }
                }
                string fsiadmvs = fsiRequest.mdtrtarea_admvs.Substring(0, 2);
                Random rnd = new Random();
                DateTime dateTime = DateTime.Now;
                var fileinfo = await _ybSettlementStmtService.StmtDetail(fsiRequest, this.GetType().Name);
                var FsiHeader = new FsiHeader<OutputStmtDetail>
                {
                    output = fileinfo,
                    infcode = 0,
                    warn_msg = null,
                    cainfo = null,
                    err_msg = "success",
                    refmsg_time = dateTime.ToString("yyyyMMddHHmmssfff"),
                    signtype = null,
                    respond_time = dateTime.ToString("yyyyMMddHHmmssfff"),
                    inf_refmsgid = $"{fsiadmvs}0000{dateTime.ToString("yyyyMMddHHmmssfff")}{rnd.Next(1000000, 10000000).ToString()}"
                };
                return Json(FsiHeader);
                */

            }
            catch (Exception ex)
            {
                string Errnum = LogHelper.GetErrornumber();
                var FsiHeader = new FsiHeader<outputsignInOut>
                {
                    infcode = -1,
                    err_msg = $"[{Errnum}]调用江苏/江西医保自定义服务失败！请检查！错误内容：{ex.Message} 异常来源：{ex.StackTrace}"
                };
                return Json(FsiHeader);
            }
        }
        
        
        
        
        /// <summary>
        /// 5301人员慢特病查询
        /// </summary>
        /// <param name="request"></param>
        /// <returns></returns>
        [Route("5301")]
        [Route("hsa-fsi-5301")]
        [HttpPost]
        public async Task<IHttpActionResult> queryPsnOpspReg(HttpRequestMessage request)
        {
            try
            {
                if (FsiDatainfo.stateFsiApi == false)
                {
                    throw new Exception("该接口已被控制台停用！请联系管理员！");
                }
                var fsiRequest = JsonConvert.DeserializeObject<FsiRequestHeader<InputQueryPsnOpspReg>>(request.Content.ReadAsStringAsync().Result);
                string fsiadmvs = fsiRequest.mdtrtarea_admvs.Substring(0, 2);
                if (FsiDatainfo.statesignon == true)
                {
                    bool sign = await _signInSignOuService.SignInStateAsync(fsiRequest.fixmedins_code, fsiRequest.opter, fsiRequest.sign_no);
                    if (sign == false)
                    {
                        throw new Exception("校验签到信息失败：未查询到有效的签到信息！");
                    }
                }
                Random rnd = new Random();
                DateTime dateTime = DateTime.Now;
                var outputQueryPsnOpspReg = await _fsiIntegratedQueryService.QueryPsnOpspReg(fsiRequest, this.GetType().Name);
                var FsiHeader = new FsiHeader<OutputQueryPsnOpspReg>
                {
                    output = outputQueryPsnOpspReg,
                    infcode = 0,
                    warn_msg = null,
                    cainfo = null,
                    err_msg = "success",
                    refmsg_time = dateTime.ToString("yyyyMMddHHmmssfff"),
                    signtype = null,
                    respond_time = dateTime.ToString("yyyyMMddHHmmssfff"),
                    inf_refmsgid = $"{fsiadmvs}0000{dateTime.ToString("yyyyMMddHHmmssfff")}{rnd.Next(1000000, 10000000).ToString()}"
                };
                return Json(FsiHeader);
            }
            catch (Exception ex)
            {
                string Errnum = LogHelper.GetErrornumber();
                var FsiHeader = new FsiHeader<outputsignInOut>
                {
                    infcode = -1,
                    err_msg = $"[{Errnum}]调用江苏/江西医保自定义服务失败！请检查！错误内容：{ex.Message} 异常来源：{ex.StackTrace}"
                };
                return Json(FsiHeader);
            }
        }
        /// <summary>
        /// 5206年度费用累计
        /// </summary>
        /// <returns></returns>
        [Route("5206")]
        [Route("hsa-fsi-5206")]
        [HttpPost]
        public async Task<IHttpActionResult> queryFixmedinsPracPsnSum(HttpRequestMessage request)
        {
            try
            {
                if (FsiDatainfo.stateFsiApi == false)
                {
                    throw new Exception("该接口已被控制台停用！请联系管理员！");
                }
                var fsiRequest = JsonConvert.DeserializeObject<FsiRequestHeader<InputQueryFixmedinsPracPsnSum>>(request.Content.ReadAsStringAsync().Result);
                string fsiadmvs = fsiRequest.mdtrtarea_admvs.Substring(0, 2);
                string psn_no = fsiRequest.Input.data.psn_no;
                if (FsiDatainfo.statesignon == true)
                {
                    bool sign = await _signInSignOuService.SignInStateAsync(fsiRequest.fixmedins_code, fsiRequest.opter, fsiRequest.sign_no);
                    if (sign == false)
                    {
                        throw new Exception("校验签到信息失败：未查询到有效的签到信息！");
                    }
                }
                Random rnd = new Random();
                DateTime dateTime = DateTime.Now;
                var outputCuminfo = await _fsiIntegratedQueryService.QueryFixmedinsPracPsnSum(fsiRequest, this.GetType().Name);
                var FsiHeader = new FsiHeader<outputCuminfo>
                {
                    output = outputCuminfo,
                    infcode = 0,
                    warn_msg = null,
                    cainfo = null,
                    err_msg = "success",
                    refmsg_time = dateTime.ToString("yyyyMMddHHmmssfff"),
                    signtype = null,
                    respond_time = dateTime.ToString("yyyyMMddHHmmssfff"),
                    inf_refmsgid = $"{fsiadmvs}0000{dateTime.ToString("yyyyMMddHHmmssfff")}{rnd.Next(1000000, 10000000).ToString()}"
                };
                return Json(FsiHeader);
            }
            catch (Exception ex)
            {
                string Errnum = LogHelper.GetErrornumber();
                var FsiHeader = new FsiHeader<outputsignInOut>
                {
                    infcode = -1,
                    err_msg = $"[{Errnum}]调用江苏/江西医保自定义服务失败！请检查！错误内容：{ex.Message} 异常来源：{ex.StackTrace}"
                };
                return Json(FsiHeader);
            }
        }

        /// <summary>
        /// 3101明细审核事前分析
        /// </summary>
        /// <param name="request"></param>
        /// <returns></returns>
        [Route("3101")]
        [Route("hsa-fsi-3101")]
        [HttpPost]
        public async Task<IHttpActionResult> beforeAnalyze(HttpRequestMessage request)
        {
            try
            {
                var fsiRequest = JsonConvert.DeserializeObject<FsiRequestHeader<InputRiskConServiceresult>>(request.Content.ReadAsStringAsync().Result);
                if (FsiDatainfo.stateFsiApi == false)
                {
                    throw new Exception("该接口已被控制台停用！请联系管理员！");
                }
                if (FsiDatainfo.statesignon == true)
                {
                    bool sign = await _signInSignOuService.SignInStateAsync(fsiRequest.fixmedins_code, fsiRequest.opter, fsiRequest.sign_no);
                    if (sign == false)
                    {
                        throw new Exception("校验签到信息失败：未查询到有效的签到信息！");
                    }
                }
                string fsiadmvs = fsiRequest.mdtrtarea_admvs.Substring(0, 2);
                Random rnd = new Random();
                DateTime dateTime = DateTime.Now;
                var outputriskConService = await _riskConService.BeforeAnalyze(fsiRequest, this.GetType().Name);
                var FsiHeader = new FsiHeader<OutputriskConService>
                {
                    output = outputriskConService,
                    infcode = 0,
                    warn_msg = null,
                    cainfo = null,
                    err_msg = "success",
                    refmsg_time = dateTime.ToString("yyyyMMddHHmmssfff"),
                    signtype = null,
                    respond_time = dateTime.ToString("yyyyMMddHHmmssfff"),
                    inf_refmsgid = $"{fsiadmvs}0000{dateTime.ToString("yyyyMMddHHmmssfff")}{rnd.Next(1000000, 10000000).ToString()}"
                };
                return Json(FsiHeader);

            }
            catch (Exception ex)
            {
                string Errnum = LogHelper.GetErrornumber();
                var FsiHeader = new FsiHeader<outputsignInOut>
                {
                    infcode = -1,
                    err_msg = $"[{Errnum}]调用江苏/江西医保自定义服务失败！请检查！错误内容：{ex.Message} 异常来源：{ex.StackTrace}"
                };
                return Json(FsiHeader);
            }
        }


        /// <summary>
        /// 3102明细审核事中分析
        /// </summary>
        /// <param name="request"></param>
        /// <returns></returns>
        [Route("3102")]
        [Route("hsa-fsi-3102")]
        [HttpPost]
        public async Task<IHttpActionResult> courseAnalyze(HttpRequestMessage request)
        {
            try
            {
                var fsiRequest = JsonConvert.DeserializeObject<FsiRequestHeader<InputRiskConServiceresult>>(request.Content.ReadAsStringAsync().Result);
                if (FsiDatainfo.stateFsiApi == false)
                {
                    throw new Exception("该接口已被控制台停用！请联系管理员！");
                }
                if (FsiDatainfo.statesignon == true)
                {
                    bool sign = await _signInSignOuService.SignInStateAsync(fsiRequest.fixmedins_code, fsiRequest.opter, fsiRequest.sign_no);
                    if (sign == false)
                    {
                        throw new Exception("校验签到信息失败：未查询到有效的签到信息！");
                    }
                }
                string fsiadmvs = fsiRequest.mdtrtarea_admvs.Substring(0, 2);
                Random rnd = new Random();
                DateTime dateTime = DateTime.Now;
                var outputriskConService = await _riskConService.CourseAnalyze(fsiRequest, this.GetType().Name);
                var FsiHeader = new FsiHeader<OutputriskConService>
                {
                    output = outputriskConService,
                    infcode = 0,
                    warn_msg = null,
                    cainfo = null,
                    err_msg = "success",
                    refmsg_time = dateTime.ToString("yyyyMMddHHmmssfff"),
                    signtype = null,
                    respond_time = dateTime.ToString("yyyyMMddHHmmssfff"),
                    inf_refmsgid = $"{fsiadmvs}0000{dateTime.ToString("yyyyMMddHHmmssfff")}{rnd.Next(1000000, 10000000).ToString()}"
                };
                return Json(FsiHeader);

            }
            catch (Exception ex)
            {
                string Errnum = LogHelper.GetErrornumber();
                var FsiHeader = new FsiHeader<outputsignInOut>
                {
                    infcode = -1,
                    err_msg = $"[{Errnum}]调用江苏/江西医保自定义服务失败！请检查！错误内容：{ex.Message} 异常来源：{ex.StackTrace}"
                };
                return Json(FsiHeader);
            }
        }

        /// <summary>
        /// 3501 盘存上传
        /// </summary>
        /// <param name="request"></param>
        /// <returns></returns>
        [Route("3501")]
        [Route("hsa-fsi-3501")]
        [HttpPost]
        public async Task<IHttpActionResult> goodsUpload(HttpRequestMessage request)
        {
            try
            {
                var fsiRequest = JsonConvert.DeserializeObject<FsiRequestHeader<InputGoodsUpload>>(request.Content.ReadAsStringAsync().Result);
                if (FsiDatainfo.stateFsiApi == false)
                {
                    throw new Exception("该接口已被控制台停用！请联系管理员！");
                }
                if (FsiDatainfo.statesignon == true)
                {
                    bool sign = await _signInSignOuService.SignInStateAsync(fsiRequest.fixmedins_code, fsiRequest.opter, fsiRequest.sign_no);
                    if (sign == false)
                    {
                        throw new Exception("校验签到信息失败：未查询到有效的签到信息！");
                    }
                }

                string fsiadmvs = fsiRequest.mdtrtarea_admvs.Substring(0, 2);
                Random rnd = new Random();
                DateTime dateTime = DateTime.Now;
                var flag = await _goodsService.GoodsUpload(fsiRequest, this.GetType().Name);
                if (flag == true)
                {
                    var FsiHeader = new FsiHeader<outputsignInOut>
                    {
                        infcode = 0,
                        warn_msg = null,
                        cainfo = null,
                        err_msg = "success",
                        refmsg_time = dateTime.ToString("yyyyMMddHHmmssfff"),
                        signtype = null,
                        respond_time = dateTime.ToString("yyyyMMddHHmmssfff"),
                        inf_refmsgid = $"{fsiadmvs}0000{dateTime.ToString("yyyyMMddHHmmssfff")}{rnd.Next(1000000, 10000000).ToString()}"
                    };
                    return Json(FsiHeader);
                }
                else
                {
                    string Errnum = LogHelper.GetErrornumber();
                    var FsiHeader = new FsiHeader<outputsignInOut>
                    {
                        infcode = -1,
                        err_msg = $"[{Errnum}]调用江苏/江西医保自定义服务失败！请检查！调用进销存上传服务GoodsService.GoodsUpload返回的状态为{flag}！"
                    };
                    return Json(FsiHeader);
                }
            }
            catch (Exception ex)
            {
                string Errnum = LogHelper.GetErrornumber();
                var FsiHeader = new FsiHeader<outputsignInOut>
                {
                    infcode = -1,
                    err_msg = $"[{Errnum}]调用江苏/江西医保自定义服务失败！请检查！错误内容：{ex.Message} 异常来源：{ex.StackTrace}"
                };
                return Json(FsiHeader);
            }

        }

        /// <summary>
        /// 3502 库存变更
        /// </summary>
        /// <param name="request"></param>
        /// <returns></returns>
        [Route("3502")]
        [Route("hsa-fsi-3502")]
        [HttpPost]
        public async Task<IHttpActionResult> goodsUpdate(HttpRequestMessage request)
        {
            try
            {
                var fsiRequest = JsonConvert.DeserializeObject<FsiRequestHeader<InputGoodsUpdate>>(request.Content.ReadAsStringAsync().Result);
                if (FsiDatainfo.stateFsiApi == false)
                {
                    throw new Exception("该接口已被控制台停用！请联系管理员！");
                }
                if (FsiDatainfo.statesignon == true)
                {
                    bool sign = await _signInSignOuService.SignInStateAsync(fsiRequest.fixmedins_code, fsiRequest.opter, fsiRequest.sign_no);
                    if (sign == false)
                    {
                        throw new Exception("校验签到信息失败：未查询到有效的签到信息！");
                    }
                }
                string fsiadmvs = fsiRequest.mdtrtarea_admvs.Substring(0, 2);
                DateTime dateTime = DateTime.Now;
                Random rnd = new Random();
                var flag = await _goodsService.GoodsUpdate(fsiRequest, this.GetType().Name);
                if (flag == true)
                {
                    var FsiHeader = new FsiHeader<outputsignInOut>
                    {
                        infcode = 0,
                        warn_msg = null,
                        cainfo = null,
                        err_msg = "success",
                        refmsg_time = dateTime.ToString("yyyyMMddHHmmssfff"),
                        signtype = null,
                        respond_time = dateTime.ToString("yyyyMMddHHmmssfff"),
                        inf_refmsgid = $"{fsiadmvs}0000{dateTime.ToString("yyyyMMddHHmmssfff")}{rnd.Next(1000000, 10000000).ToString()}"
                    };
                    return Json(FsiHeader);
                }
                else
                {
                    string Errnum = LogHelper.GetErrornumber();
                    var FsiHeader = new FsiHeader<outputsignInOut>
                    {
                        infcode = -1,
                        err_msg = $"[{Errnum}]调用江苏/江西医保自定义服务失败！请检查！调用进销存上传服务GoodsService.GoodsUpdate返回的状态为{flag}！"
                    };
                    return Json(FsiHeader);
                }

            }
            catch (Exception ex)
            {
                string Errnum = LogHelper.GetErrornumber();
                var FsiHeader = new FsiHeader<outputsignInOut>
                {
                    infcode = -1,
                    err_msg = $"[{Errnum}]调用江苏/江西医保自定义服务失败！请检查！错误内容：{ex.Message} 异常来源：{ex.StackTrace}"
                };
                return Json(FsiHeader);
            }
        }

        /// <summary>
        /// 3503 采购上传
        /// </summary>
        /// <param name="request"></param>
        /// <returns></returns>
        [Route("3503")]
        [Route("hsa-fsi-3503")]
        [HttpPost]
        public async Task<IHttpActionResult> goodsBuy(HttpRequestMessage request)
        {
            try
            {
                var fsiRequest = JsonConvert.DeserializeObject<FsiRequestHeader<InputGoodsBuy>>(request.Content.ReadAsStringAsync().Result);
                if (FsiDatainfo.stateFsiApi == false)
                {
                    throw new Exception("该接口已被控制台停用！请联系管理员！");
                }
                if (FsiDatainfo.statesignon == true)
                {
                    bool sign = await _signInSignOuService.SignInStateAsync(fsiRequest.fixmedins_code, fsiRequest.opter, fsiRequest.sign_no);
                    if (sign == false)
                    {
                        throw new Exception("校验签到信息失败：未查询到有效的签到信息！");
                    }
                }
                string fsiadmvs = fsiRequest.mdtrtarea_admvs.Substring(0, 2);
                Random rnd = new Random();
                DateTime dateTime = DateTime.Now;
                var flag = await _goodsService.GoodsBuy(fsiRequest, this.GetType().Name);
                if (flag == true)
                {
                    var FsiHeader = new FsiHeader<outputsignInOut>
                    {
                        infcode = 0,
                        warn_msg = null,
                        cainfo = null,
                        err_msg = "success",
                        refmsg_time = dateTime.ToString("yyyyMMddHHmmssfff"),
                        signtype = null,
                        respond_time = dateTime.ToString("yyyyMMddHHmmssfff"),
                        inf_refmsgid = $"{fsiadmvs}0000{dateTime.ToString("yyyyMMddHHmmssfff")}{rnd.Next(1000000, 10000000).ToString()}"
                    };
                    return Json(FsiHeader);
                }
                else
                {
                    string Errnum = LogHelper.GetErrornumber();
                    var FsiHeader = new FsiHeader<outputsignInOut>
                    {
                        infcode = -1,
                        err_msg = $"[{Errnum}]调用江苏/江西医保自定义服务失败！请检查！调用进销存上传服务GoodsService.GoodsBuy返回的状态为{flag}！"
                    };
                    return Json(FsiHeader);
                }
            }
            catch (Exception ex)
            {
                string Errnum = LogHelper.GetErrornumber();
                var FsiHeader = new FsiHeader<outputsignInOut>
                {
                    infcode = -1,
                    err_msg = $"[{Errnum}]调用江苏/江西医保自定义服务失败！请检查！错误内容：{ex.Message} 异常来源：{ex.StackTrace}"
                };
                return Json(FsiHeader);
            }
        }


        /// <summary>
        /// 3504 采购退货上传
        /// </summary>
        /// <param name="request"></param>
        /// <returns></returns>
        [Route("3504")]
        [Route("hsa-fsi-3504")]
        [HttpPost]
        public async Task<IHttpActionResult> goodsBuyReturn(HttpRequestMessage request)
        {
            try
            {
                var fsiRequest = JsonConvert.DeserializeObject<FsiRequestHeader<InputGoodsBuy>>(request.Content.ReadAsStringAsync().Result);
                if (FsiDatainfo.stateFsiApi == false)
                {
                    throw new Exception("该接口已被控制台停用！请联系管理员！");
                }
                if (FsiDatainfo.statesignon == true)
                {
                    bool sign = await _signInSignOuService.SignInStateAsync(fsiRequest.fixmedins_code, fsiRequest.opter, fsiRequest.sign_no);
                    if (sign == false)
                    {
                        throw new Exception("校验签到信息失败：未查询到有效的签到信息！");
                    }
                }
                string fsiadmvs = fsiRequest.mdtrtarea_admvs.Substring(0, 2);
                Random rnd = new Random();
                DateTime dateTime = DateTime.Now;
                var flag = await _goodsService.GoodsBuyReturn(fsiRequest, this.GetType().Name);
                if (flag == true)
                {
                    var FsiHeader = new FsiHeader<outputsignInOut>
                    {
                        infcode = 0,
                        warn_msg = null,
                        cainfo = null,
                        err_msg = "success",
                        refmsg_time = dateTime.ToString("yyyyMMddHHmmssfff"),
                        signtype = null,
                        respond_time = dateTime.ToString("yyyyMMddHHmmssfff"),
                        inf_refmsgid = $"{fsiadmvs}0000{dateTime.ToString("yyyyMMddHHmmssfff")}{rnd.Next(1000000, 10000000).ToString()}"
                    };
                    return Json(FsiHeader);
                }
                else
                {
                    string Errnum = LogHelper.GetErrornumber();
                    var FsiHeader = new FsiHeader<outputsignInOut>
                    {
                        infcode = -1,
                        err_msg = $"[{Errnum}]调用江苏/江西医保自定义服务失败！请检查！调用进销存上传服务GoodsService.GoodsBuyReturn返回的状态为{flag}！"
                    };
                    return Json(FsiHeader);
                }
            }
            catch (Exception ex)
            {
                string Errnum = LogHelper.GetErrornumber();
                var FsiHeader = new FsiHeader<outputsignInOut>
                {
                    infcode = -1,
                    err_msg = $"[{Errnum}]调用江苏/江西医保自定义服务失败！请检查！错误内容：{ex.Message} 异常来源：{ex.StackTrace}"
                };
                return Json(FsiHeader);
            }
        }

        /// <summary>
        /// 3505 销售上传
        /// </summary>
        /// <param name="request"></param>
        /// <returns></returns>
        [Route("3505")]
        [Route("hsa-fsi-3505")]
        [HttpPost]
        public async Task<IHttpActionResult> goodsSell(HttpRequestMessage request)
        {
            try
            {
                var fsiRequest = JsonConvert.DeserializeObject<FsiRequestHeader<InputGoodsSell>>(request.Content.ReadAsStringAsync().Result);
                if (FsiDatainfo.stateFsiApi == false)
                {
                    throw new Exception("该接口已被控制台停用！请联系管理员！");
                }
                if (FsiDatainfo.statesignon == true)
                {
                    bool sign = await _signInSignOuService.SignInStateAsync(fsiRequest.fixmedins_code, fsiRequest.opter, fsiRequest.sign_no);
                    if (sign == false)
                    {
                        throw new Exception("校验签到信息失败：未查询到有效的签到信息！");
                    }
                }
                string fsiadmvs = fsiRequest.mdtrtarea_admvs.Substring(0, 2);
                Random rnd = new Random();
                DateTime dateTime = DateTime.Now;
                var flag = await _goodsService.GoodsSell(fsiRequest, this.GetType().Name);
                if (flag == true)
                {
                    var FsiHeader = new FsiHeader<outputsignInOut>
                    {
                        infcode = 0,
                        warn_msg = null,
                        cainfo = null,
                        err_msg = "success",
                        refmsg_time = dateTime.ToString("yyyyMMddHHmmssfff"),
                        signtype = null,
                        respond_time = dateTime.ToString("yyyyMMddHHmmssfff"),
                        inf_refmsgid = $"{fsiadmvs}0000{dateTime.ToString("yyyyMMddHHmmssfff")}{rnd.Next(1000000, 10000000).ToString()}"
                    };
                    return Json(FsiHeader);
                }
                else
                {
                    string Errnum = LogHelper.GetErrornumber();
                    var FsiHeader = new FsiHeader<outputsignInOut>
                    {
                        infcode = -1,
                        err_msg = $"[{Errnum}]调用江苏/江西医保自定义服务失败！请检查！调用进销存上传服务GoodsService.GoodsSell返回的状态为{flag}！"
                    };
                    return Json(FsiHeader);
                }
            }
            catch (Exception ex)
            {
                string Errnum = LogHelper.GetErrornumber();
                var FsiHeader = new FsiHeader<outputsignInOut>
                {
                    infcode = -1,
                    err_msg = $"[{Errnum}]调用江苏/江西医保自定义服务失败！请检查！错误内容：{ex.Message} 异常来源：{ex.StackTrace}"
                };
                return Json(FsiHeader);
            }
        }

        /// <summary>
        /// 3506 销售退货
        /// </summary>
        /// <param name="request"></param>
        /// <returns></returns>
        [Route("3506")]
        [Route("hsa-fsi-3506")]
        [HttpPost]
        public async Task<IHttpActionResult> goodsSellReturn(HttpRequestMessage request)
        {
            try
            {
                var fsiRequest = JsonConvert.DeserializeObject<FsiRequestHeader<InputGoodsSell>>(request.Content.ReadAsStringAsync().Result);
                if (FsiDatainfo.stateFsiApi == false)
                {
                    throw new Exception("该接口已被控制台停用！请联系管理员！");
                }
                if (FsiDatainfo.statesignon == true)
                {
                    bool sign = await _signInSignOuService.SignInStateAsync(fsiRequest.fixmedins_code, fsiRequest.opter, fsiRequest.sign_no);
                    if (sign == false)
                    {
                        throw new Exception("校验签到信息失败：未查询到有效的签到信息！");
                    }
                }
                string fsiadmvs = fsiRequest.mdtrtarea_admvs.Substring(0, 2);
                Random rnd = new Random();
                DateTime dateTime = DateTime.Now;
                var flag = await _goodsService.GoodsSellReturn(fsiRequest, this.GetType().Name);
                if (flag == true)
                {
                    var FsiHeader = new FsiHeader<outputsignInOut>
                    {
                        infcode = 0,
                        warn_msg = null,
                        cainfo = null,
                        err_msg = "success",
                        refmsg_time = dateTime.ToString("yyyyMMddHHmmssfff"),
                        signtype = null,
                        respond_time = dateTime.ToString("yyyyMMddHHmmssfff"),
                        inf_refmsgid = $"{fsiadmvs}0000{dateTime.ToString("yyyyMMddHHmmssfff")}{rnd.Next(1000000, 10000000).ToString()}"
                    };
                    return Json(FsiHeader);
                }
                else
                {
                    string Errnum = LogHelper.GetErrornumber();
                    var FsiHeader = new FsiHeader<outputsignInOut>
                    {
                        infcode = -1,
                        err_msg = $"[{Errnum}]调用江苏/江西医保自定义服务失败！请检查！调用进销存上传服务GoodsService.GoodsSellReturn返回的状态为{flag}！"
                    };
                    return Json(FsiHeader);
                }
            }
            catch (Exception ex)
            {
                string Errnum = LogHelper.GetErrornumber();
                var FsiHeader = new FsiHeader<outputsignInOut>
                {
                    infcode = -1,
                    err_msg = $"[{Errnum}]调用江苏/江西医保自定义服务失败！请检查！错误内容：{ex.Message} 异常来源：{ex.StackTrace}"
                };
                return Json(FsiHeader);
            }
        }

        /// <summary>
        /// 3507 上传记录删除
        /// </summary>
        /// <param name="request"></param>
        /// <returns></returns>
        [Route("3507")]
        [Route("hsa-fsi-3507")]
        [HttpPost]
        public async Task<IHttpActionResult> goodsInfoDelete(HttpRequestMessage request)
        {
            try
            {
                var fsiRequest = JsonConvert.DeserializeObject<FsiRequestHeader<InputGoodsInfoDelete>>(request.Content.ReadAsStringAsync().Result);
                if (FsiDatainfo.stateFsiApi == false)
                {
                    throw new Exception("该接口已被控制台停用！请联系管理员！");
                }
                if (FsiDatainfo.statesignon == true)
                {
                    bool sign = await _signInSignOuService.SignInStateAsync(fsiRequest.fixmedins_code, fsiRequest.opter, fsiRequest.sign_no);
                    if (sign == false)
                    {
                        throw new Exception("校验签到信息失败：未查询到有效的签到信息！");
                    }
                }
                string fsiadmvs = fsiRequest.mdtrtarea_admvs.Substring(0, 2);
                Random rnd = new Random();
                DateTime dateTime = DateTime.Now;
                var flag = await _goodsService.GoodsInfoDelete(fsiRequest, this.GetType().Name);
                if (flag == true)
                {
                    var FsiHeader = new FsiHeader<outputsignInOut>
                    {
                        infcode = 0,
                        warn_msg = null,
                        cainfo = null,
                        err_msg = "success",
                        refmsg_time = dateTime.ToString("yyyyMMddHHmmssfff"),
                        signtype = null,
                        respond_time = dateTime.ToString("yyyyMMddHHmmssfff"),
                        inf_refmsgid = $"{fsiadmvs}0000{dateTime.ToString("yyyyMMddHHmmssfff")}{rnd.Next(1000000, 10000000).ToString()}"
                    };
                    return Json(FsiHeader);
                }
                else
                {
                    string Errnum = LogHelper.GetErrornumber();
                    var FsiHeader = new FsiHeader<outputsignInOut>
                    {
                        infcode = -1,
                        err_msg = $"[{Errnum}]调用江苏/江西医保自定义服务失败！请检查！调用进销存上传服务GoodsService.GoodsInfoDelete返回的状态为{flag}！"
                    };
                    return Json(FsiHeader);
                }
            }
            catch (Exception ex)
            {
                string Errnum = LogHelper.GetErrornumber();
                var FsiHeader = new FsiHeader<outputsignInOut>
                {
                    infcode = -1,
                    err_msg = $"[{Errnum}]调用江苏/江西医保自定义服务失败！请检查！错误内容：{ex.Message} 异常来源：{ex.StackTrace}"
                };
                return Json(FsiHeader);
            }
        }

        /// <summary>
        /// 9101文件上传
        /// </summary>
        /// <param name="request"></param>
        /// <returns></returns>
        [Route("9101")]
        [Route("hsa-fsi-9101")]
        [HttpPost]
        public async Task<IHttpActionResult> upload(HttpRequestMessage request)
        {
            try
            {
                var fsiRequest = JsonConvert.DeserializeObject<FsiRequestHeader<InputFileupload>>(request.Content.ReadAsStringAsync().Result);
                if (FsiDatainfo.stateFsiApi == false)
                {
                    throw new Exception("该接口已被控制台停用！请联系管理员！");
                }
                if (FsiDatainfo.statesignon == true)
                {
                    bool sign = await _signInSignOuService.SignInStateAsync(fsiRequest.fixmedins_code, fsiRequest.opter, fsiRequest.sign_no);
                    if (sign == false)
                    {
                        throw new Exception("校验签到信息失败：未查询到有效的签到信息！");
                    }
                }

                string fsiadmvs = fsiRequest.mdtrtarea_admvs.Substring(0, 2);
                Random rnd = new Random();
                DateTime dateTime = DateTime.Now;
                var outputFileUpload = await _fileuploadService.Upload(fsiRequest, this.GetType().Name);
                var FsiHeader = new FsiHeader<OutputFileUpload>
                {
                    output = outputFileUpload,
                    infcode = 0,
                    warn_msg = null,
                    cainfo = null,
                    err_msg = "success",
                    refmsg_time = dateTime.ToString("yyyyMMddHHmmssfff"),
                    signtype = null,
                    respond_time = dateTime.ToString("yyyyMMddHHmmssfff"),
                    inf_refmsgid = $"{fsiadmvs}0000{dateTime.ToString("yyyyMMddHHmmssfff")}{rnd.Next(1000000, 10000000).ToString()}"
                };
                return Json(FsiHeader);

            }
            catch (Exception ex)
            {
                string Errnum = LogHelper.GetErrornumber();
                var FsiHeader = new FsiHeader<outputsignInOut>
                {
                    infcode = -1,
                    err_msg = $"[{Errnum}]调用江苏/江西医保自定义服务失败！请检查！错误内容：{ex.Message} 异常来源：{ex.StackTrace}"
                };
                return Json(FsiHeader);
            }
        }

        /// <summary>
        /// 9102文件下载
        /// </summary>
        /// <param name="request"></param>
        /// <returns></returns>
        [Route("9102")]
        [Route("hsa-fsi-9102")]
        [HttpPost]
        public async Task<IHttpActionResult> download(HttpRequestMessage request)
        {
            try
            {
                var fsiRequest = JsonConvert.DeserializeObject<FsiRequestHeader<InputFileDownload>>(request.Content.ReadAsStringAsync().Result);
                if (FsiDatainfo.stateFsiApi == false)
                {
                    throw new Exception("该接口已被控制台停用！请联系管理员！");
                }
                if (FsiDatainfo.statesignon == true)
                {
                    bool sign = await _signInSignOuService.SignInStateAsync(fsiRequest.fixmedins_code, fsiRequest.opter, fsiRequest.sign_no);
                    if (sign == false)
                    {
                        throw new Exception("校验签到信息失败：未查询到有效的签到信息！");
                    }
                }
                string fsiadmvs = fsiRequest.mdtrtarea_admvs.Substring(0, 2);
                Random rnd = new Random();
                DateTime dateTime = DateTime.Now;
                var output = await _fileuploadService.Download(fsiRequest, this.GetType().Name);
                return ResponseMessage(output);
            }
            catch (Exception ex)
            {
                string Errnum = LogHelper.GetErrornumber();
                var FsiHeader = new FsiHeader<outputsignInOut>
                {
                    infcode = -1,
                    err_msg = $"[{Errnum}]调用江苏/江西医保自定义服务失败！请检查！错误内容：{ex.Message} 异常来源：{ex.StackTrace}"
                };
                return Json(FsiHeader);
            }
        }

    }
}
